
-- 檢視遊戲清單
local httparg               = require("shared.api.httparg")
local response              = require("shared.api.response")
local config                = require("config")
local tag                   = httparg.tag()
local assertion             = httparg.assertion
local util                  = require "proxy.game.common.util"
local fill_response_header  = util.fill_response_header
local is_match_content_type = util.is_match_content_type

local cjson                 = require "cjson.safe"
      cjson.encode_max_depth(32)
      cjson.decode_max_depth(32)

local ARGS  = {
  tag.body('json', 'required'),

  brand     = tag.json.brand    ('string',assertion.non_empty_string()),
  status    = tag.json.status   ('string',assertion.non_empty_string()),
  locale    = tag.json.locale   ('string',assertion.non_empty_string()),
  title     = tag.json.title    ('string',assertion.non_empty_string()),
  game_code = tag.json.game_code('string',assertion.non_empty_string()),
  category  = tag.json.category ('string',assertion.non_empty_string()),
  count     = tag.json.count    ('number',assertion.non_nan_nor_inf(),assertion.non_negative_number()),
  offset    = tag.json.offset   ('number',assertion.non_nan_nor_inf(),assertion.non_negative_number()),
}

local result ={}

do
  local httpclient = require "shared.http.client"
  local location   = config.ENV.DATAGATE_SERVICE .. "/game"
  local headers    = ngx.req.get_headers()
  local query      = nil

  do
      headers['X-Forwarded-For'] = ngx.var.proxy_add_x_forwarded_for
  end


  --移除方法设定
  local rmvLowKey = "x-http-method-override"
  --移除gzip设定
  local rmvKey2   = "accept-encoding"
  for k,v in pairs(headers) do
      local low = string.lower(k)
      if low==rmvLowKey  or low==rmvKey2   then
          headers[k] = nil
      end
  end


  local method   = "VIEW"
  local bodyMap  = {
                   brands       = {ARGS.brand},
                   namespaces   = {"primary"},
                   status       = ARGS.status,
                   locale       = ARGS.locale,
                   title        = ARGS.title,
                   game_code    = ARGS.game_code,
                   category     = ARGS.category,
                   count        = ARGS.count,
                   offset       = ARGS.offset,
                   }

  local requestData = cjson.encode(bodyMap)
  local resp, err   = httpclient.new()
                              :uri(location)
                              :headers(headers)
                              :query(query)
                              :body(requestData)
                              :send(method, 2000, nil)
  if err or (not resp) then
      ngx.status = 500
      ngx.say(err)
      ngx.exit(ngx.OK)
  end

  if not resp.body
          or not resp.headers
          or resp.status ~= 200
          or not is_match_content_type(resp, 'application/json')  then

      ngx.status = resp.status
      fill_response_header(resp.headers)
      ngx.say (resp.body)
      ngx.exit(ngx.OK)
  end

  local respBody
  local get_content_decoder = util.get_content_decoder
  local contentDecoder      = get_content_decoder(resp)
  if contentDecoder then
        respBody = contentDecoder.decode(resp.body)
  else

      ngx.status = 500
      ngx.say(err)
      ngx.exit(ngx.OK)
  end

  result.content = respBody.content
end
  response.success(result)